//
//  HJSearchController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/26.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJSearchController.h"
#import "HJSearchResultsController.h"
#import "HJSearchNavBar.h"
#import "HJSearchModel.h"
#import "HJSearchCell.h"

@interface HJSearchController ()<searchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)NSArray * sectionArr;
@property(nonatomic,strong) HJSearchModel * searchModel;

@property(nonatomic,strong)UICollectionView * collectionView;

@property(nonatomic,strong)HJSearchNavBar * navBar;

@end

@implementation HJSearchController

-(NSArray *)sectionArr {
    if (!_sectionArr) {
        _sectionArr = @[@"历史搜索",@"热门搜索"];
    }
    return _sectionArr;
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
     [self.navigationController setNavigationBarHidden:YES];
    [self loadNetworkData];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNaviTitleView];
    [self configCollectionView];
    self.searchModel =[[HJSearchModel alloc]init];
}

-(void)configNaviTitleView {
    self.navBar = [[HJSearchNavBar alloc]init];
    self.navBar.frame = CGRectMake(0, 0, mScreenWidth, 94);
    self.navBar.backBtn.hidden = NO;
    self.navBar.searchBtn.hidden = YES;
    self.navBar.delegate = self;
    [self.view addSubview:self.navBar];
}
-(void)searchBarTextReturnWithText:(NSString *)text {
    if ([CommonTools isEmpty:text]) {
        [self showErrorHUDWithError:@"内容不能为空!"];
    }else {
        [self pushSearchResultsWithTitle:text];
    }
}
-(void)searchBarBackBtnClicked {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)configCollectionView {
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.headerReferenceSize = CGSizeMake(mScreenWidth, 50.0f);
    layout.footerReferenceSize = CGSizeZero;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 98, mScreenWidth, mScreenHeight-98) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HJSearchCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    
    [self.view addSubview:self.collectionView];
}

-(void)loadNetworkData {
    [TZHttpTool post:HotSearchAPI params:nil success:^(NSDictionary *result) {
        self.searchModel = [HJSearchModel mj_objectWithKeyValues:result[@"data"]];
        [self.collectionView reloadData];
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (self.searchModel.history.count>0) {
        return 2;
    }
    return 1;
}
//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.searchModel.history.count>0) {
        if (section==0) {
            return self.searchModel.history.count;
        }else {
            return self.searchModel.hot_words.count;
        }
    }else {
        return self.searchModel.hot_words.count;
    }
   
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HJSearchCell * cell = (HJSearchCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    if (self.searchModel.history.count>0) {
        if (indexPath.section==0) {
            history * histModel =self.searchModel.history[indexPath.row];
            cell.title.text = histModel.search_words;
            cell.backgroundColor = [UIColor redColor];
        }else {
            hot_words * hotModel =self.searchModel.hot_words[indexPath.row];
            cell.title.text = hotModel.hot_words;
        }
    }else {
        hot_words * hotModel =self.searchModel.hot_words[indexPath.row];
        cell.title.text = hotModel.hot_words;
    }
    
    return cell;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(5, 15, 5, 7.5);
}
//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}


- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
   
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        if (!headerView) {
            headerView = [UICollectionReusableView new];
        }
        UILabel *label = [headerView viewWithTag:111];
        if (!label) {
            label = [[UILabel alloc] initWithFrame:CGRectMake(mScreenWidth /375 * 15, 30, 120, 20)];
            label.tag = 111;
            label.textColor =HJColorRGB(0);
            label.textAlignment = NSTextAlignmentLeft;
            label.font = [UIFont systemFontOfSize:17];
            [headerView addSubview:label];
        }
        UIButton * sectionBtn = [headerView viewWithTag:10];
        if (!sectionBtn) {
            UIButton * sectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            sectionBtn.frame = CGRectMake(mScreenWidth - 15 - 40, CGRectGetMinY(label.frame), 50, label.height);
            [sectionBtn setTitleColor:HJColorRGB(0) forState:UIControlStateNormal];
            sectionBtn.alpha = 0.5;
            sectionBtn.titleLabel.textAlignment = NSTextAlignmentRight;
            sectionBtn.tag = 10;
            sectionBtn.titleLabel.font =HJFontSize(13);
            [sectionBtn addTarget:self action:@selector(sectionBtnClicked) forControlEvents:UIControlEventTouchUpInside];
            [headerView addSubview:sectionBtn];
        }
        if (self.searchModel.history.count>0) {
            if (indexPath.section == 0) {
                label.text = @"历史搜索";
                [sectionBtn setTitle:@"清除" forState:UIControlStateNormal];
            }else {
                label.text = @"热门搜索";
                [sectionBtn setTitle:@"" forState:UIControlStateNormal];
            }
        }else {
            label.text = @"热门搜索";
            [sectionBtn setTitle:@"" forState:UIControlStateNormal];
        }
        return headerView;
    }else {
        return nil;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.searchModel.history.count>0) {
        if (indexPath.section==0) {
            history * histModel =self.searchModel.history[indexPath.row];
           return CGSizeMake(histModel.cellW, 40);
        }else {
            hot_words * hotModel =self.searchModel.hot_words[indexPath.row];
            return CGSizeMake(hotModel.cellW, 40);
        }
    }else {
        hot_words * hotModel =self.searchModel.hot_words[indexPath.row];
        return CGSizeMake(hotModel.cellW, 40);
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.searchModel.history.count>0) {
        if (indexPath.section==0) {
            history * histModel =self.searchModel.history[indexPath.row];
            [self pushSearchResultsWithTitle:histModel.search_words];
        }else {
            hot_words * hotModel =self.searchModel.hot_words[indexPath.row];
            [self pushSearchResultsWithTitle:hotModel.hot_words];
        }
    }else {
        hot_words * hotModel =self.searchModel.hot_words[indexPath.row];
        [self pushSearchResultsWithTitle:hotModel.hot_words];
    }
}


-(void)pushSearchResultsWithTitle:(NSString *)title {
    HJSearchResultsController *vc = [[HJSearchResultsController alloc]init];
    vc.str = title;
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)sectionBtnClicked {
    [TZHttpTool post:DeleteHistoryAPI params:nil success:^(NSDictionary *result) {
        [self loadNetworkData];
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}
@end
